package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.PartnerDto;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.dto.PartnerQuery;
import com.heima.pojo.vo.PartnerVo;
import com.heima.pojo.vo.PageVo;
import com.heima.service.IPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author 存折
 * @since 2024-10-17
 */
@Slf4j
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, PartnerDto> implements IPartnerService {

    @Autowired
    private PartnerMapper partnerMapper;

    @Override
    public PageVo<PartnerVo> query(PartnerQuery partnerQuery) {

        String name = partnerQuery.getName();
        Page page = new Page<>(partnerQuery.getPageIndex(), partnerQuery.getPageSize());
        Page<PartnerDto> p = lambdaQuery()
                .like(name != null, PartnerDto::getName, name)
                .page(page);
        long total = p.getTotal();
        long pages = p.getPages();
        List<PartnerDto> records = p.getRecords();
        List<PartnerVo> partnerVoList = new ArrayList<>();
        records.forEach(partnerDto -> {
            PartnerVo partnerVo = new PartnerVo();
            BeanUtils.copyProperties(partnerDto, partnerVo);
            partnerVo.setVmCount(partnerMapper.selectNum(partnerVo.getId()));
            partnerVoList.add(partnerVo);
        });
        return new PageVo<PartnerVo>(partnerQuery.getPageIndex(), partnerQuery.getPageSize(),pages,total, partnerVoList);
        //1.你要什么数据
        //2.从哪里取到你要的数据
    }
}
